package vuelta.general.stage;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import vuelta.general.BaseActivity;
import vuelta.general.R;
import vuelta.general.adapter.SaliteDetailGalleryAdapter;
import vuelta.general.stage.zoom.SaliteDetailZoomActivity;
import vuelta.general.util.Commons;
import vuelta.general.util.Parser;
import android.content.Intent;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Gallery;
import android.widget.TextView;

public class SaliteActivity extends BaseActivity {
	private SaliteActivity nSaliteActivity;
	private SaliteDetailGalleryAdapter mSaliteDetailGalleryAdapter;
	private Gallery mStageDetailGallery;
	private TextView mIndexFormattedText;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.salite_gallery);
		initToolbar();
		// Parser parser = new Parser();
		// mStagesArray = parser.createArrayJson(this, R.raw.tappe);
		nSaliteActivity = this;

		JSONArray jsonArray = Commons.getInstance().getTappeArrayJson();
		int stageNumber = Commons.getInstance().getSelectItemPosition();
		JSONArray saliteJsonArray = Parser.getInstance().getAttributeFromJsonStage(jsonArray, stageNumber, "salite");

		mSaliteDetailGalleryAdapter = new SaliteDetailGalleryAdapter(this, saliteJsonArray);
		mStageDetailGallery = (Gallery) findViewById(R.id.stages_gallery);
		mStageDetailGallery.setAdapter(mSaliteDetailGalleryAdapter);

		mHandler.post(new Runnable() {

			public void run() {
				mSaliteDetailGalleryAdapter.notifyDataSetChanged();
			}
		});

		mIndexFormattedText = (TextView) findViewById(R.id.salite_detail_index_text);
		mStageDetailGallery.setOnTouchListener(new View.OnTouchListener() {

			public boolean onTouch(View v, MotionEvent event) {
				if (event.getAction() == MotionEvent.ACTION_UP) {
					mIndexFormattedText.postDelayed(new Runnable() {

						public void run() {
							mIndexFormattedText.setText(Integer.toString((1 + mStageDetailGallery
									.getSelectedItemPosition())));
						}
					}, 200);
				}
				return false;
			}
		});

		mIndexFormattedText.setText(Integer.toString(1));

		TextView index_total_text = (TextView) findViewById(R.id.salite_detail_total_text);
		index_total_text.setText(Integer.toString(mSaliteDetailGalleryAdapter.getCount()));

		Button dettaglioButton = (Button) findViewById(R.id.dettaglio_button);
		dettaglioButton.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				int position = mStageDetailGallery.getSelectedItemPosition();
				JSONObject o = (JSONObject) mSaliteDetailGalleryAdapter.getItem(position);
				
				Intent dettaglioIntent = new Intent(nSaliteActivity, SaliteDetailZoomActivity.class);
				try {
					dettaglioIntent.putExtra("salita", o.getString("dafile"));
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				nSaliteActivity.startActivity(dettaglioIntent);
			}
		});

	}

}
